package org.fall.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.logging.log4j.util.Strings;
import org.fall.system.constant.BackstageConstant;
import org.fall.system.dao.AdminUserMapper;
import org.fall.system.exception.DeleteFailedException;
import org.fall.system.model.ResultEntity;
import org.fall.system.model.dto.AdminUserDTO;
import org.fall.system.model.dto.UserQueryDTO;
import org.fall.system.model.po.AdminUserPO;
import org.fall.system.model.vo.AdminUserVO;
import org.fall.system.service.BackstageAdminUserService;
import org.fall.system.utils.AdminCommonTool;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zjh
 * @since 2021/4/1 22:26
 */
@Service
public class BackstageAdminUserServiceImpl implements BackstageAdminUserService {

    @Resource
    private AdminUserMapper adminUserMapper;

    @Override
    public List<AdminUserVO> queryAdminUserList(UserQueryDTO dto) {
        QueryWrapper<AdminUserPO> queryWrapper = new QueryWrapper<>();
        if (Strings.isNotBlank(dto.getStartDate())) {
            queryWrapper.ge("gmt_create", dto.getStartDate());
        }
        if (Strings.isNotBlank(dto.getStartDate())) {
            queryWrapper.le("gmt_create", dto.getEndDate());
        }
        if (Strings.isNotBlank(dto.getKeyword())) {
            queryWrapper.and(true, (queryWrapper1) -> queryWrapper1.like("username", dto.getKeyword()).or().like("name", dto.getKeyword()));
        }
        List<AdminUserPO> adminUserPoList = adminUserMapper.selectList(queryWrapper);
        List<AdminUserVO> adminUserVoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(adminUserPoList)) {
            for (AdminUserPO po : adminUserPoList) {
                AdminUserVO vo = new AdminUserVO();
                BeanUtils.copyProperties(po, vo);
                adminUserVoList.add(vo);
            }
        }
        return adminUserVoList;
    }

    @Override
    public ResultEntity deleteAdminUser(String username) {
        // 根据username删除管理员用户
        int deleteNum = adminUserMapper.delete(new QueryWrapper<AdminUserPO>().eq("username", username));
        if (deleteNum == 1) {
            return ResultEntity.successWithoutData();
        }
        return ResultEntity.failed(BackstageConstant.DELETE_FAILED);
    }

    @Override
    public ResultEntity changeAdminUserState(String username) {
        QueryWrapper<AdminUserPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        AdminUserPO po = adminUserMapper.selectOne(queryWrapper);
        po.setStatus(po.getStatus() == 1 ? 0 : 1);
        int updateNum = adminUserMapper.update(po, queryWrapper);
        if (updateNum == 1) {
            return ResultEntity.successWithoutData();
        }
        return ResultEntity.failed(BackstageConstant.CHANGE_STATE_FAILED);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultEntity batchDeleteAdminUsers(List<Integer> list) {
        int count = adminUserMapper.deleteBatchIds(list);
        if (list.size() == count) {
            return ResultEntity.successWithoutData();
        } else {
            // 删除失败，抛出异常，事务回滚
            throw new DeleteFailedException();
        }
    }

    @Override
    public AdminUserVO getAdminUserById(Integer id) {
        AdminUserPO po = adminUserMapper.selectById(id);
        if (po == null) {
            return null;
        }
        AdminUserVO vo = new AdminUserVO();
        BeanUtils.copyProperties(po, vo);
        return vo;
    }

    @Override
    public ResultEntity editAdminUser(AdminUserDTO dto) {
        QueryWrapper<AdminUserPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", dto.getUsername());
        AdminUserPO po = new AdminUserPO();
        BeanUtils.copyProperties(dto, po);
        po.setRole(AdminCommonTool.getRoleByLevel(po.getLevel()));
        int updateNum = adminUserMapper.update(po, queryWrapper);
        if (updateNum == 1) {
            return ResultEntity.successWithoutData();
        }
        return ResultEntity.failed(BackstageConstant.UPDATE_FAILED);
    }

}
